function createForecast() {
	//从高德地图获得数据。
	var reponse = new XMLHttpRequest();
	reponse.onreadystatechange = function () {
		if (reponse.readyState == 4 && reponse.status == 200) { //判断成功返回
			//console.log(reponse.responseText);
			var resualt = JSON.parse(reponse.responseText);
			if (resualt.status == 1 && resualt.infocode == 10000) { 
				//判断数据读取是否成功
				console.log("从高德加载数据。");
				localStorage.setItem("forecast", JSON.stringify(resualt.forecasts[0])); 
				//存储forecast变量,减少forecast请求次数
			}
		}
	}
	reponse.onerror = function () {
		console.log("数据加载失败！");
		return null;
	}
	reponse.open("GET",
		"https://restapi.amap.com/v3/weather/weatherInfo?key=32483671a996dc0250095ed77a0e1926&city=350124&extensions=all&output=JSON",
		true);
	reponse.setRequestHeader("Accept", "application/json");
	reponse.send();
}
function getForecast() { //获得变量forecast
	var forecast;
	console.log("数据加载开始！");
	if (localStorage.getItem("forecast") == undefined) createForecast();
	if (localStorage.getItem("forecast") != undefined) {
		forecast = JSON.parse(localStorage.getItem("forecast")); //从localStorage获得天气数据
		if (Number(new Date()) - Number(new Date(forecast.reporttime)) <= 60 * 60 * 1000){ //判断时间是否过长未更新。60分钟更新一次
			console.log("从本地加载数据。");
			//console.log(localStorage.getItem("forecast"));
		}
		else {
			createForecast();
			forecast = JSON.parse(localStorage.getItem("forecast"));
		}
	}
	return forecast;
}

var day = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"];
var dates = ["今天  ", "明天  ", "后天  ", "大后  ", "第五天"];
function getTextHelp(i, casts) {
	return dates[i] + day[casts[i].week - 1] + ""+
		" 天气 白天:" + casts[i].dayweather + "-> 晚上:" + casts[i].nightweather +
		"</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+
		" 温度 白天:" + casts[i].daytemp + "℃-> 晚上:" + casts[i].nighttemp + "℃";
}

function getText() {
	var forecast = getForecast();
	//console.log("输出后 :" + JSON.stringify(forecast));
	if(forecast==undefined) return "";
	var str = forecast.province + "省" + forecast.city + ":\n" + "<hr color='blue'>";
	var casts = forecast.casts;
	for (let i = 0; i < 2; i++) {
		if (i != 0) str += "</br>";
		str += getTextHelp(i, casts);
	}
	return str;
}
